package com.atguigu.atcrowdfunding.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.atguigu.atcrowdfunding.bean.TAdmin;
import com.atguigu.atcrowdfunding.bean.TAdminExample;
import com.atguigu.atcrowdfunding.bean.TAdminExample.Criteria;
import com.atguigu.atcrowdfunding.exception.LoginException;
import com.atguigu.atcrowdfunding.mapper.TAdminMapper;
import com.atguigu.atcrowdfunding.service.TAdminService;
import com.atguigu.atcrowdfunding.util.AppDateUtils;
import com.atguigu.atcrowdfunding.util.Const;
import com.atguigu.atcrowdfunding.util.MD5Util;
import com.github.pagehelper.PageInfo;

@Service
public class TAdminServiceImpl implements TAdminService {

	@Autowired
	TAdminMapper adminMapper;

	@Override
	public TAdmin getTAdminByLogin(Map<String, Object> paramMap) {
//		查询用户
		String loginacct = (String)paramMap.get("loginacct");
		String userpswd = (String)paramMap.get("userpswd");
//		判断用户是否为空
		TAdminExample example = new TAdminExample();
		
		example.createCriteria().andLoginacctEqualTo(loginacct);
		
		List<TAdmin> list = adminMapper.selectByExample(example);
		
//		查询不到用户名
		if(list == null || list.size() == 0) {
			throw new LoginException(Const.LOGIN_LOGINACCT_ERROR);
		}
		TAdmin admin = list.get(0);
//		判断密码是否一致,密码加密
		if(!admin.getUserpswd().equals(MD5Util.digest(userpswd))) {
//			密码不一致
			throw new LoginException(Const.LOGIN_USERPSWD_ERROR);
		}
//		返回结果
		return admin;
	}

	@Override
	public PageInfo<TAdmin> listAdminPage(Map<String, Object> paramMap) {
		String condition = (String) paramMap.get("condition");
		TAdminExample example = new TAdminExample();
		if (!"".equals(condition)) {
			example.createCriteria().andLoginacctLike("%"+condition+"%");
			
			Criteria criteria2 = example.createCriteria();
			criteria2.andUsernameLike("%"+condition+"%");
			
			Criteria criteria3 = example.createCriteria();
			criteria3.andEmailLike("%"+condition+"%");
			
			example.or(criteria2);
			example.or(criteria3);
		}
		example.setOrderByClause("createtime desc");
		List<TAdmin> list = adminMapper.selectByExample(example);
		PageInfo<TAdmin> page = new PageInfo<TAdmin>(list,5);
		return page;
	}

	@Override
	@Transactional
	public void saveTAdmin(TAdmin admin) {
//		有选择性的保存 动态sql
		admin.setUserpswd(MD5Util.digest(Const.DEFAULT_USERPSWD));
		admin.setCreatetime(AppDateUtils.getFormatTime());
		adminMapper.insertSelective(admin);
	}

	@Override
	public TAdmin getTAdminById(Integer id) {
		return adminMapper.selectByPrimaryKey(id);
	}

	@Override
	@Transactional
	public void updateTAdmin(TAdmin admin) {
		adminMapper.updateByPrimaryKeySelective(admin);
	}

	@Override
	@Transactional
	public void deleteTAdmin(Integer id) {
		adminMapper.deleteByPrimaryKey(id);
	}

	@Override
	@Transactional
	public void deleteBatch(List<Integer> idList) {
		adminMapper.deleteBatch(idList);
	}

}
